package com.um.jdy.platform.station.manager;

import com.um.jdy.common.device.code.DeviceCode;
import com.um.jdy.common.device.entity.dto.StationSharingDTO;
import com.um.jdy.common.device.entity.po.Station;
import com.um.jdy.common.device.entity.po.StationSharing;
import com.um.jdy.common.device.service.StationService;
import com.um.jdy.common.device.service.StationSharingService;
import com.um.jdy.common.platform.code.PartnersCode;
import com.um.jdy.platform.station.entity.dto.SharingDTO;
import com.um.jdy.platform.station.entity.dto.StationSharingPageDTO;
import com.um.jdy.platform.station.entity.dto.UserSharingDTO;
import com.um.jdy.platform.station.entity.vo.StationSharingItemVO;
import com.um.jdy.platform.station.entity.vo.StationSharingListItemVO;
import com.um.jdy.platform.station.entity.vo.StationSharingUserItemVO;
import com.um.jdy.platform.station.entity.vo.UserSharingItemVO;
import com.um.jdy.platform.user.service.SessionUserService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 管理类：站场
 *
 * @author cs
 * 日期 2023/5/24
 * 版权所有 广州优匠科技有限公司
 */
@Component
public class StationSharingManager extends BaseManager<StationSharingService> {

    @Autowired
    SessionUserService sessionUserService;

    @Autowired
    StationService stationService;


    /**
     * 分页查询分成网点
     * @param params
     * @return
     */
    public Response<Page<StationSharingItemVO>> getPage(StationSharingPageDTO params){
        Page<StationSharingItemVO> page = new Page<>();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());


        page = service.getViewModelPageBySqlName(page, "get_station_sharing_page","get_station_sharing_count",StationSharingItemVO.class,
                new Object[]{getComId(),getComId(),StringUtil.getQueryParam(params.getName())});
        page.getResult().stream().forEach(item -> {
            List<UserSharingItemVO> users = service.getViewModelList("get_sharing_users_list",UserSharingItemVO.class,
                    new Object[]{getComId(),item.getDeviceStationId()});
            item.setUsers(users);
        });

        return Response.success(page);
    }

    /**
     * 分页查询分成用户
     * @param params
     * @return
     */
    public Response<Page<StationSharingUserItemVO>> getUserPage(StationSharingPageDTO params){
        Page<StationSharingUserItemVO> page = new Page<>();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());


        page = service.getViewModelPageBySqlName(page, "get_sharing_user_page","get_sharing_user_count",StationSharingUserItemVO.class,
                new Object[]{getComId(),getComId(),StringUtil.getQueryParam(params.getName())});
        page.getResult().stream().forEach(item -> {
            List<StationSharingListItemVO> stations = service.getViewModelList("get_sharing_stations_list",StationSharingListItemVO.class,
                    new Object[]{getComId(),item.getPlatformMerchantId()});
            item.setStations(stations);
        });

        return Response.success(page);
    }


    /**
     * 根据手机号搜索用户
     * @param dto
     * @return
     */
    public Response<List<UserSharingItemVO>> getSharingListByParentId(UserSharingDTO dto) {
        List<UserSharingItemVO> list = service.getViewModelList("get_station_sharing_list_by_parentid", UserSharingItemVO.class,
                new Object[]{dto.getStationId(),getComId(),dto.getMerchantId(),getComId()});
        return Response.success(list);
    }

    @Transactional
    public Response<Station> saveEntity(SharingDTO dto) {
        int sum = 0;
        for(StationSharingDTO item : dto.getList()){
            sum += item.getCommissionRate();
        }

        Station station = stationService.getEntity(dto.getDeviceStationId());
        if(station == null){
            throw new ServiceException(DeviceCode.NoStation);
        }

        if(sum > station.getCommissionRate().intValue()){
            throw new ServiceException(PartnersCode.RateNotLargeThanStation);
        }

        //先删除
        service.delete("device_station_id",dto.getDeviceStationId());

        int sortNo = 1;
        for(StationSharingDTO stationSharingDTO : dto.getList()){
            StationSharing entity = UMBeanUtils.copyBean(stationSharingDTO,StationSharing.class);
            entity.setCreateTime(new Date());
            entity.setDeviceStationId(dto.getDeviceStationId());
            entity.setSortNo(sortNo);
            sortNo ++;
            service.addEntity(entity);
        }
        return Response.success();
    }

    public Response delete(String id) {
        boolean result = this.service.delete("device_station_id",id);
        return result ? Response.success() : Response.failed();
    }


    @Transactional
    public Response deleteIds(List<String> ids) {
        ids.stream().forEach(id -> {
            this.service.delete("device_station_id",id);
        });
        return Response.success();
    }

}
